﻿@using CGT.Infrastructure.Entity
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@{
    var roleList = (ViewBag.RoleList as List<GovRole>) ?? new List<GovRole>();
    var userInfo = (ViewBag.UserInfo as GovUser) ?? new GovUser();
}
@section style{
    <style>
        .form-horizontal .control-label {
            width: 20%;
            float: left;
            text-align: right;
        }

        .col-sm-3 {
            width: 80%;
            float: left;
        }
    </style>
    <link href="@Url.Content("~/css/plat/blue.css")" rel="stylesheet">
}
<form id="form1" class="form-horizontal">
    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="box-body">
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="name">用户姓名：</label>
                        <div class="col-sm-3">
                            <input class="form-control" style="width:300px;" maxlength="50" name="txtUserName" id="txtUserName" value="@userInfo.RealName" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="name">身份证明号码：</label>
                        <div class="col-sm-3">
                            <input class="form-control" style="width:300px;" maxlength="18" name="txtIdNumber" id="txtIdNumber" value="@userInfo.IdentityCode" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="name">是否为警员：</label>
                        <div class="col-sm-3">
                            @if (userInfo.IsPolice == 0)
                            {
                                <div>
                                    <label class="radio-inline">
                                        <input type="radio" name="radioIsPolice" value="1" /> 是
                                    </label>
                                    <label class="radio-inline">
                                        <input type="radio" name="radioIsPolice" value="0" checked="checked" /> 否
                                    </label>
                                </div>
                            }
                            else
                            {
                                <div>
                                    <label class="radio-inline">
                                        <input type="radio" name="radioIsPolice" value="1" checked="checked" /> 是
                                    </label>
                                    <label class="radio-inline">
                                        <input type="radio" name="radioIsPolice" value="0" /> 否
                                    </label>
                                </div>
                            }
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="name">警员/员工编号：</label>
                        <div class="col-sm-3">
                            <input class="form-control" style="width:300px;" maxlength="50" name="txtEmployeeNo" id="txtEmployeeNo" value="@userInfo.UserNumber" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="name">登录帐号：</label>
                        <div class="col-sm-3">
                            <input class="form-control" style="width:300px;" maxlength="50" name="txtLoginName" id="txtLoginName" value="@userInfo.UserName" />
                        </div>
                    </div>
                    @if (userInfo.UserID <= 0)
                    {
                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="name">初始密码：</label>
                            <div class="col-sm-3">
                                <input class="form-control" style="width:300px;" maxlength="50" name="txtInitPassword" id="txtInitPassword" />
                            </div>
                        </div>
                    }
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="name">允许登录的IP：</label>
                        <div class="col-sm-3">
                            <textarea class="form-control" rows="3" style="width:300px;" maxlength="50" name="txtAllowIP" id="txtAllowIP" placeholder="多个IP英文逗号间隔，不填为不做限制">@userInfo.AllowIP</textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="name">允许登录的时间段：</label>
                        <div class="col-sm-3">
                            @if (userInfo.UserID > 0)
                            {
                                <div>
                                    <input class="form-inline" style="width:30px;" maxlength="2" name="txtAllowStartTime" id="txtAllowStartTime" value="@userInfo.LoginStartTime" />时 -
                                    <input class="form-inline" style="width:30px;" maxlength="2" name="txtAllowEndTime" id="txtAllowEndTime" value="@userInfo.LoginEndTime" />时
                                </div>
                            }
                            else
                            {
                                <div>
                                    <input class="form-inline" style="width:30px;" maxlength="2" name="txtAllowStartTime" id="txtAllowStartTime" />时 -
                                    <input class="form-inline" style="width:30px;" maxlength="2" name="txtAllowEndTime" id="txtAllowEndTime" />时
                                </div>
                            }
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="name">用户有效期至：</label>
                        <div class="col-sm-3">
                            @if (userInfo.UserID > 0)
                            {
                                <input class="form-control" rows="3" style="width:300px;" maxlength="50" name="UserIndate" id="UserIndate" value="@userInfo.UserExpireDate.ToString("yyyy-MM-dd")" />
                            }
                            else
                            {
                                <input class="form-control" rows="3" style="width:300px;" maxlength="50" name="UserIndate" id="UserIndate" value="" />
                            }
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="name">密码有效期至：</label>
                        <div class="col-sm-3">
                            @if (userInfo.UserID > 0)
                            {
                                <input class="form-control" rows="3" style="width:300px;" maxlength="50" name="PwdIndate" id="PwdIndate" value="@userInfo.PasswordExpireDate.ToString("yyyy-MM-dd")" />
                            }
                            else
                            {
                                <input class="form-control" rows="3" style="width:300px;" maxlength="50" name="PwdIndate" id="PwdIndate" value="" />
                            }
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="name">用户角色：</label>
                        <div class="col-sm-3">
                            <select name="RoleList" id="RoleList" style="width:150px" class="form-control">
                                @foreach (var item in roleList)
                                {
                                    if (item.RoleID == userInfo.RoleId)
                                    {
                                        <option selected value="@item.RoleID">@item.RoleName</option>
                                    }
                                    else
                                    {
                                        <option value="@item.RoleID">@item.RoleName</option>
                                    }
                                }
                            </select>
                        </div>
                    </div>


                </div>
                <div class="box-footer text-center">
                    <button class="btn btn-primary" type="button" id="btnSave">确 定 </button>
                    <button class="btn" type="button" id="cancel">取 消 </button>
                </div>
            </div>

        </div>
        <!-- /.row -->
    </section>

</form>
@section scriptsBottom{
    <script src="@Url.Content("~/js/icheck.min.js")"></script>
    <script type="text/javascript">
        var userId = @userInfo.UserID; //是否是添加页面，如果是编辑页则判断条件增加ID
        $(document).ready(function () {
            initFormValidate();
            binds();
            $('input').iCheck({
                radioClass: 'iradio_flat-blue'
            });
        });

        //保存
        function save() {
            var bootstrapValidator = $("#form1").data('bootstrapValidator');
            bootstrapValidator.validate();
            if (bootstrapValidator.isValid()) {
                //验证用户有效期和密码有效期
                var userInDate = $("#UserIndate").val();
                var pwdInDate = $("#PwdIndate").val();
                if (userInDate == null || $.trim(userInDate) == "") {
                    BMCW.alert("请输入用户有效期");
                    return;
                }
                if (pwdInDate == null || $.trim(pwdInDate) == "") {
                    BMCW.alert("请输入密码有效期");
                    return;
                }
                var dateFormat = /^(\d{4})-(\d{2})-(\d{2})$/;
                if (!dateFormat.test(userInDate)) {
                    BMCW.alert("用户有效期格式不正确，正确的格式为2008-01-01");
                    return;
                }
                if (!dateFormat.test(pwdInDate)) {
                    BMCW.alert("密码有效期格式不正确，正确的格式为2008-01-01");
                    return;
                }
                var options = {
                    url: '@Url.Action("SaveUser", "User")',
                    type: "POST",
                    data: {
                        UserID:@userInfo.UserID,
                        UserName: $("#txtLoginName").val(),
                        PassWord: $("#txtInitPassword").val(),
                        RealName: $("#txtUserName").val(),
                        IdentityCode: $("#txtIdNumber").val(),
                        IsPolice: $("input[name='radioIsPolice']:checked").val(),

                        UserNumber: $("#txtEmployeeNo").val(),
                        UserExpireDate: $("#UserIndate").val(),
                        PasswordExpireDate: $("#PwdIndate").val(),
                        LoginStartTime: $("#txtAllowStartTime").val(),
                        LoginEndTime: $("#txtAllowEndTime").val(),
                        AllowIP: $("#txtAllowIP").val(),
                        RoleId: $("#RoleList").val()
                    },
                    success: function (o) {
                        if (o.status == 0) {
                            BMCW.msg("保存成功", function () {
                                BMCW.getTopSelectedWin().search();
                            });
                        } else {
                            BMCW.alert("保存失败");
                        }
                    }
                };

                BMCW.ajax(options);
            }
        }
        function binds() {
            //保存
            $("#btnSave").click(function () {
                save();
            });
            //取消
            $("#cancel").click(function () {
                BMCW.closeTopLayer();
            });
            $("#UserIndate").datepicker({
                language: "zh-CN",
                autoclose: true,//选中之后自动隐藏日期选择框
                format: "yyyy-mm-dd"//日期格式，
            });
            $("#PwdIndate").datepicker({
                language: "zh-CN",
                autoclose: true,//选中之后自动隐藏日期选择框
                format: "yyyy-mm-dd"//日期格式，
            });


            $("#txtEmployeeNo").blur(function () {
               checkNumIsExist()
            });
            $("#txtLoginName").blur(function () {
              checkLoginNameIsExist();
            });
            $("#txtIdNumber").blur(function () {
               checkIdNumIsExist();
            });
        }

        //检查员工编号是否存在
        function checkNumIsExist() {
            var url ="@Url.Action("IsExistNo", "User")";
            url += "?userId=" + userId + "&userNumber=" + $("#txtEmployeeNo").val() + "&isPolice=" + $("input[name='radioIsPolice']:checked").val();
            var ajaxOption = {
                url: url,
                async: false,
                type: "GET",
                success: function (result) {
                    if (result.data == "1") {
                        $("#form1").data("bootstrapValidator").updateStatus("txtEmployeeNo","INVALID","exits");
                        BMCW.alert("员工/警员编号已经存在");
                    } else {
                    }
                }
            };
            BMCW.ajax(ajaxOption);
        }

        //检查身份证是否存在
        function checkIdNumIsExist() {
            var url = "@Url.Action("IsExistIdNum", "User")";
            url += "?userId=" + userId + "&idnum=" + $("#txtIdNumber").val()
            var ajaxOption = {
                url: url,
                type: "GET",
                async: false,
                success: function (result) {
                    if (result.data == "1") {
                        $("#form1").data("bootstrapValidator").updateStatus("txtIdNumber","INVALID","exits");
                        BMCW.alert("身份证明号码已经存在");
                    } else {
                    }
                }
            };
            BMCW.ajax(ajaxOption);
        }

        //检查登录用户名是否存在
        function checkLoginNameIsExist() {
            //登录名不可编辑，只有新增时才验证
            if (userId <= 0) {
                var url = "@Url.Action("IsExistLoginName", "User")";
                url += "?userId=" + userId + "&loginName=" + $("#txtLoginName").val();
                var ajaxOption = {
                    url: url,
                    type: "Get",
                    success: function (result) {
                        if (result.data == "1") {
                            $("#form1").data("bootstrapValidator").updateStatus("txtLoginName","INVALID","exits");
                            BMCW.alert("登录帐号已经存在");
                        } else {
                        }
                    }
                };
                BMCW.ajax(ajaxOption);
            }
        }

        function initFormValidate() {
            $("#form1").bootstrapValidator({
                submitHandler: function (valiadtor, loginForm, submitButton) {
                    valiadtor.defaultSubmit();
                },
                fields: {
                    txtUserName: {
                        validators: {
                            notEmpty: {
                                message: '请输入用户姓名'
                            }
                        }
                    },
                    txtIdNumber: {
                        validators: {
                            notEmpty: {
                                message: '请输入身份证明号码'
                            },
                            regexp: {
                                regexp: /^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|31)|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}([0-9]|x|X)$/,
                                message: '请输入正确的身份证明号码'
                            },
                            exits:{
                                message:"身份证明号码已经存在"
                            }
                        }
                    },
                    txtEmployeeNo: {
                        validators: {
                            notEmpty: {
                                message: '请输入员工/警员编号'
                            },
                            exits:{
                                message:"员工/警员编号已经存在"
                            }
                        }
                    },
                    txtLoginName: {
                        validators: {
                            notEmpty: {
                                message: '请输入登录帐号'
                            },
                            exits:{
                                message:"登录帐号已经存在"
                            }
                        }
                    },
                    txtInitPassword: {
                        validators: {
                            notEmpty: {
                                message: '请输入初始化密码'
                            },
                            regexp:{
                                regexp:/^(?![0-9a-z]+$)(?![0-9A-Z]+$)(?![0-9\W]+$)(?![a-z\W]+$)(?![a-zA-Z]+$)(?![A-Z\W]+$)[a-zA-Z0-9\W_]{8,}$/,
                                message:'密码格式不正确（长度不小于8位，且必须包含英文字符、数字及特殊符号）'
                            }
                        }
                    },
                    txtAllowIP: {
                        callback:{
                            message:"请输入正确的IP地址",
                            callback: function(value, validator) {
                                if (value == "*")
                                    return true;
                                var ips = value.split(',');
                                var result = true;
                                for (var i = 0; i < ips.length; i++) {
                                    var ip = ips[i];
                                    result = this.optional(element) || /^(([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5])))\.)(([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5])))\.){2}([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5])))$/.test(ip);
                                    if (result == false)
                                        break;
                                }
                                return result;
                            }
                        }
                    },

                    //UserIndate: {
                    //    validators: {
                    //        notEmpty: {
                    //            message: '用户有效期(单位为月份)'
                    //        }
                    //    }
                    //},
                    //PwdIndate: {
                    //    validators: {
                    //        notEmpty: {
                    //            message: '密码有效期(单位为月份)'
                    //        }
                    //    }
                    //},
                    RoleList: {
                        validators: {
                            notEmpty: {
                                message: '请选择用户角色'
                            }
                        }
                    },
                    txtAllowStartTime: {
                        validators: {
                            notEmpty: {
                                message: '请输入允许登录时间'
                            },
                            regexp: {
                                regexp: /^[0-9]*$/,
                                message: '请输入数字'
                            },
                            callback:{
                                message:"请输入一个介于 0 和 24 之间的值",
                                callback: function(value, validator) {
                                    if(parseInt(value)>=0 && parseInt(value)<=24){
                                        return true;
                                    }
                                    return false;
                                }
                            }
                        }
                    },
                    txtAllowEndTime: {
                        validators: {
                            notEmpty: {
                                message: '请输入允许登录时间'
                            },
                            regexp: {
                                regexp: /^[1-9][0-9]*$/,
                                message: '请输入数字(大于0)'
                            },
                            callback:{
                                message:"请输入一个介于 0 和 24 之间的值",
                                callback: function(value, validator) {
                                    if(parseInt(value)>=0 && parseInt(value)<=24){
                                        return true;
                                    }
                                    return false;
                                }
                            }
                        }
                    },

                }
            });
        }
    </script>
}


